package com.sskj.uboex.http;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.util.MD5Util;
import com.sskj.level.bean.TradeListBean;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.StockCoinBean;
import com.sskj.mine.bean.CoinFeeBean;
import com.sskj.mine.bean.OtherRecordBean;
import com.sskj.mine.bean.RechargeListBean;
import com.sskj.uboex.bean.AppVersionBean;
import com.sskj.uboex.bean.AssetBean;
import com.sskj.uboex.bean.Banner;
import com.sskj.uboex.bean.ChartViewCj;
import com.sskj.uboex.bean.KFiveBean;
import com.sskj.uboex.bean.LevelVolumeBean;
import com.sskj.uboex.bean.Notice;
import com.sskj.uboex.bean.NoticeList;
import com.sskj.uboex.bean.RechargeInfoBean;
import com.sskj.uboex.bean.SocketSendBean;

import java.util.List;

import okhttp3.WebSocket;


/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-15 14:08
 */
public class HttpService implements IHttpService {

    @Override
    public PostRequest<HttpData<Notice>> ggDetail(String id) {
        return OkGo.<HttpData<Notice>>post(HttpConfig.BASE_URL + HttpConfig.NOTICE_DETAIL)
                .params("id", id);
    }

    @Override
    public GetRequest<HttpData<KFiveBean>> getTradeEntrustList(int page, int pageSize, String type, String code, String tradeType) {
        return OkGo.<HttpData<KFiveBean>>get(HttpConfig.BASE_URL + HttpConfig.ENTRUST_LIST)
                .params("page", page)
                .removeParam("id")
                .params("pageSize", pageSize)
                .params("code", code);
    }

    @Override
    public GetRequest<HttpData<PageBean<TradeListBean>>> getTradeList(int page, int pageSize, String type, String code, String tradeType) {
        return OkGo.<HttpData<PageBean<TradeListBean>>>get(com.sskj.level.http.HttpConfig.BASE_URL + com.sskj.level.http.HttpConfig.TRADE_POSITION_LIST)
                .params("page", page)
                .params("pageSize", pageSize)
                .params("stockCode", code)
                .removeParam("id")
                .params("tradeType", tradeType)
                .params("type", type);
    }

    /**
     * 充值
     *
     * @param code 币种码
     * @param type 类型 ETH 1，BTC 2，BCH 3
     * @return
     */
    @Override
    public PostRequest<HttpData<RechargeInfoBean>> recharge(String code, String type) {
        return OkGo.<HttpData<RechargeInfoBean>>post(HttpConfig.BASE_URL + HttpConfig.RECHARGE_URL)
                .params("code", code)
                .params("type", type);
    }

    /**
     * 获取资产列表
     *
     * @return
     */
    @Override
    public PostRequest<HttpData<List<AssetBean>>> getAssets() {
        return OkGo.<HttpData<List<AssetBean>>>post(HttpConfig.BASE_URL + HttpConfig.ASSET_LIST);
    }

    @Override
    public PostRequest<HttpData<Banner>> getBannerImage() {
        return OkGo.<HttpData<Banner>>post(HttpConfig.BASE_URL + HttpConfig.COIN_FEE)
                .params("type", 10);
    }

    @Override
    public PostRequest<HttpData<PageBean<NoticeList>>> getGGList(String page, String pageSize) {
        return OkGo.<HttpData<PageBean<NoticeList>>>post(HttpConfig.BASE_URL + HttpConfig.NOTICE)
                .params("page", page)
                .params("pageSize", pageSize)
                ;
    }

    @Override
    public GetRequest<HttpData<ChartViewCj>> getLevelVolumeList(String code, int page) {
        return OkGo.<HttpData<ChartViewCj>>get(HttpConfig.BASE_URL + HttpConfig.TRADE_LEVEL_VOLUME)
                .params("stockCode", code)
                .params("page", page)
                .params("pageSize", 20);
    }

    @Override
    public GetRequest<HttpData<List<LevelVolumeBean>>> getLevelVolumeListBb(String code, int page) {
        return OkGo.<HttpData<List<LevelVolumeBean>>>get(HttpConfig.BASE_URL + HttpConfig.TRADE_LEVEL_VOLUME_BB)
                .params("code", code)
                .params("page", page)
                .params("pageSize", 20);
    }

    @Override
    public PostRequest<HttpData<List<StockCoinBean>>> getProduct(String code) {
        return OkGo.<HttpData<List<StockCoinBean>>>post(HttpConfig.BASE_URL + HttpConfig.PRODUCT_LIST)
                .params("type", 4)
                .params("code", code)
                .params("coinArea", code == null ? "2" : (code.contains("/") ? null : "1"));
    }

    @Override
    public WebSocketObserver pushCoinType(String type) {
        return new WebSocketObserver(HttpConfig.WS_STOCK) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response) {
                webSocket.send(new Gson().toJson(new SocketSendBean(type)));
            }
        };
    }

    @Override
    public WebSocketObserver pushCoinFive(String type) {
        return new WebSocketObserver(HttpConfig.WS_STOCK) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response) {
                webSocket.send(new Gson().toJson(new SocketSendBean(type)));
            }
        };
    }

    @Override
    public PostRequest<HttpData<AppVersionBean>> getVersion() {
        return OkGo.<HttpData<AppVersionBean>>post(HttpConfig.BASE_URL + HttpConfig.GET_VERSION)
                .params("appType", 2);

    }

    @Override
    public PostRequest<HttpData<List<LevelVolumeBean>>> getLevelVolumeListBbArea(String code, int page) {
        return OkGo.<HttpData<List<LevelVolumeBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_VOLUME_AREA)
                .params("code", code);
    }

    /**
     * 冲提币记录
     *
     * @return
     */
    @Override
    public GetRequest<HttpData<PageBean<RechargeListBean>>> getAssetRecord(int page, int pageSize, String type, String stockCode) {
        return OkGo.<HttpData<PageBean<RechargeListBean>>>get(com.sskj.mine.http.HttpConfig.BASE_URL + com.sskj.mine.http.HttpConfig.ASSET_RECORD)
                .params("page", page)
                .params("pageSize", pageSize)
                .params("type", type)
                .params("stockCode", stockCode);
    }

    @Override
    public PostRequest<HttpData<PageBean<OtherRecordBean>>> getOtherRecord(int page, int pageSize, String stockCode) {
        return OkGo.<HttpData<PageBean<OtherRecordBean>>>post(com.sskj.mine.http.HttpConfig.BASE_URL + com.sskj.mine.http.HttpConfig.OTHER_RECORD)
                .params("page", page)
                .params("pageSize", pageSize)
                .params("stockCode", stockCode);
    }

    /**
     * 获取短信验证码---codeType 1代表注册  2代表重置密码
     *
     * @param mobile   手机号
     * @param type
     * @param codeType
     * @param account  账号
     * @return
     */
    @Override
    public GetRequest<HttpData> sendCode(String mobile, String type, String codeType, String account) {
        return OkGo.<HttpData>get(com.sskj.mine.http.HttpConfig.BASE_URL + com.sskj.mine.http.HttpConfig.SMS_SEND)
                .params("codeType", codeType)
                .params("account", account);
    }

    /**
     * 提现
     *
     * @param code       验证码
     * @param tradePwd   交易密码
     * @param usdFee     金额
     * @param walletAddr 钱包地址
     * @param stockCode  币种码
     * @return
     */
    @Override
    public PostRequest<HttpData> withdraw(String code, String tradePwd, String usdFee, String walletAddr, String stockCode) {
        return OkGo.<HttpData>post(com.sskj.mine.http.HttpConfig.BASE_URL + com.sskj.mine.http.HttpConfig.WITHDRAW)
                .params("code", code)
                .params("tradePwd", MD5Util.getMd5Value(tradePwd))
                .params("usdFee", usdFee)
                .params("walletAddr", walletAddr)
                .params("stockCode", stockCode);
    }

    /**
     * 获取提现手续费
     *
     * @param code 币种码
     * @return
     */
    @Override
    public PostRequest<HttpData<CoinFeeBean>> getWithdrawFee(String code) {
        return OkGo.<HttpData<CoinFeeBean>>post(com.sskj.mine.http.HttpConfig.BASE_URL + com.sskj.mine.http.HttpConfig.GET_COIN_FEE)
                .params("code", code);
    }

    @Override
    public GetRequest<HttpData<KFiveBean>> getTradeNewList(int page, int pageSize, String type, String code, String tradeType) {

        return OkGo.<HttpData<KFiveBean>>get(HttpConfig.BASE_URL + HttpConfig.NEW_LIST)
                .params("code", code)
                .params("page", page)
                .removeParam("id")

                .params("pageSize", pageSize);
    }

    @Override
    public PostRequest<HttpData> onePay(String num) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.ONE_PAY)
                .params("fee", num);
    }

    @Override
    public GetRequest<HttpData<String>> getRMB() {
        return OkGo.<HttpData<String>>get(HttpConfig.BASE_URL + HttpConfig.GET_RMB);
    }

}